<?php

namespace app\index\service;

class WxworkService
{

    public function get_token(){
        $corpid='ww3a75fb7ecb01335c';
        $corpsecret='etv7S1IxF0t-yWVEOcFfPzzPQ5fzTsvuHBU3leWI7OY';
        $access_token = [];
        include 'access_token.php';

        if (time() > $access_token['expires']){
            $access_token = [];
            $access_token['access_token'] = $this->get_new_token($corpid,$corpsecret);
            $access_token['expires']=time()+7000;

            $arr = '<?php'.PHP_EOL.'$access_token = '.var_export($access_token,true).';'.PHP_EOL.'?>';
            $arrfile = fopen("./access_token.php","w");
            fwrite($arrfile,$arr);
            fclose($arrfile);
            return $access_token['access_token'];
        }else{
            return $access_token['access_token'];
        }
    }

    public function get_new_token($corpid,$corpsecret){
        $url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={$corpid}&corpsecret={$corpsecret}";
        $access_token_Arr =  $this->https_request($url);
        if($access_token_Arr['errcode'] == 0){
            return $access_token_Arr['access_token'];
        }
        return '';
    }

    public function https_request ($url){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        $out = curl_exec($ch);
        curl_close($ch);
        return  json_decode($out,true);
    }

    public function get_department(){
        $departmentid = 0;
        $arr = json_decode($this->curl_get("https://qyapi.weixin.qq.com/cgi-bin/department/list?access_token=".$this->get_token()), true);
        if(isset($arr['errcode']) && $arr['errcode'] == 0){
            foreach ($arr['department'] as $vv){
                if($vv['parentid'] == 0){
                    $departmentid = $vv['id'];
                    break;
                }
            }
        }
        return $departmentid;
    }

    public function get_userinfo(){
        $arr = json_decode($this->curl_get("https://qyapi.weixin.qq.com/cgi-bin/user/list?access_token=".$this->get_token()."&department_id=".$this->get_department()."&fetch_child=1"), true);
        if(isset($arr['errcode']) && $arr['errcode'] == 0){
            return $arr['userlist'];
        }
        return [];
    }



    public function send($data){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token='.$this->get_token());
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        return curl_exec($ch);
    }

    public function curl_get($url){
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HEADER, 0);
        curl_setopt($curl, CURLOPT_TIMEOUT, 1);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        return curl_exec($curl);
    }



    public function send_text($touser, $page='', $title='', $content='', $time=''){

        $content_item = [
            [
                'key' => '类型',
                'value' => $title,
            ],
            [
                'key' => '内容',
                'value' => $content,
            ],
            [
                'key' => '时间',
                'value' => $time,
            ]
        ];

        $postdata = [
            'touser' => $touser,
            'msgtype' => 'miniprogram_notice',
            'miniprogram_notice' => [
                'appid' => 'wx0093fb3869174faa',
                'page' => $page,
                'title' => $title,
                'content_item' => $content_item
            ]
        ];
        return '';
        return (new WxworkService())->send(json_encode($postdata));
    }


}
